package com.example.sunmiv1.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.EditSet;
import com.example.sunmiv1.entity.SkuGoodsInfo;
import com.example.sunmiv1.utils.net.ACache;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.example.sunmiv1.activity.good.GoodsManageActivity.sellcountBean;

public class SkuGoodsAdapter extends BaseAdapter {
    private ACache aCache;
    private Context context;
    private List<SkuGoodsInfo> list;
    private ViewHolder viewHolder;
    private boolean isRefrsh;

    public SkuGoodsAdapter(Context context, List<SkuGoodsInfo> list,boolean isRefrsh1) {
        this.context = context;
        this.list = list;
        this.isRefrsh=isRefrsh1;
        aCache = ACache.get(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = View.inflate(context, R.layout.sku_goods_adapter, null);
        viewHolder = new ViewHolder(convertView);
            String name = "";
            for (int i = 0; i < list.get(position).getName().size(); i++) {
                name = name + list.get(position).getName().get(i) + ",";
            }

            viewHolder.skuGoodsStatus.setChecked("1".equals(list.get(position).getStatus()));
            viewHolder.skuGoodsStatus.setOnCheckedChangeListener((buttonView, isChecked) -> {
                if (isChecked) {
                    list.get(position).setStatus("1");
                } else {
                    list.get(position).setStatus("0");
                }
            });
            name = name.substring(0, name.length() - 1);
            if (getisShopVip()) {
                viewHolder.skuGoodsShopVipLin.setVisibility(View.VISIBLE);
            } else {
                viewHolder.skuGoodsShopVipLin.setVisibility(View.GONE);
            }

            if (getisRetailVip()) {
                viewHolder.skuGoodsRetailVipLin.setVisibility(View.VISIBLE);
            } else {
                viewHolder.skuGoodsRetailVipLin.setVisibility(View.GONE);
            }

            if (getisPlusVip()) {
                viewHolder.skuGoodsPlusVipLin.setVisibility(View.VISIBLE);
            } else {
                viewHolder.skuGoodsPlusVipLin.setVisibility(View.GONE);
            }

        if (getisWeight()) {
            viewHolder.skuGoodsStock.setInputType(8194);
            viewHolder.skuGoodsOver.setInputType(8194);
            viewHolder.skuGoodsTint.setInputType(8194);
            setTextListener2(viewHolder.skuGoodsOver, position, "moreNum");
            setTextListener2(viewHolder.skuGoodsTint, position, "lessNum");
        }else {
            viewHolder.skuGoodsStock.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
            viewHolder.skuGoodsOver.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
            viewHolder.skuGoodsTint.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
            setTextListener(viewHolder.skuGoodsOver, position, "moreNum");
            setTextListener(viewHolder.skuGoodsTint, position, "lessNum");
        }

        if (getisGuide()){
            viewHolder.llGuideRoyaltyOnlinePrice.setVisibility(View.VISIBLE);
            viewHolder.llGuideRoyaltyPrice.setVisibility(View.VISIBLE);
        }else {
            viewHolder.llGuideRoyaltyOnlinePrice.setVisibility(View.GONE);
            viewHolder.llGuideRoyaltyPrice.setVisibility(View.GONE);
        }

            setTextListener1(viewHolder.skuGoodsShopVipPrice, position, "shopVipPrice");
            setTextListener(viewHolder.skuGoodsShopVipIntergal, position, "shopVipIntegral");
            setTextListener1(viewHolder.skuGoodsRetailVipPrice, position, "VipPrice");
            setTextListener(viewHolder.skuGoodsRetailVipIntergal, position, "VipIntegral");
            setTextListener1(viewHolder.skuGoodsPlusVipPrice, position, "plusVipPrice");
            setTextListener(viewHolder.skuGoodsPlusVipIntergal, position, "plusVipIntegral");
            setTextListener(viewHolder.skuGoodsPluCode, position, "pluCode");

            viewHolder.skuGoodsShopVipPrice.setText(list.get(position).getShop_mem_price());
            viewHolder.skuGoodsShopVipIntergal.setText(list.get(position).getShop_mem_integral());
            viewHolder.skuGoodsRetailVipPrice.setText(list.get(position).getStore_mem_price());
            viewHolder.skuGoodsRetailVipIntergal.setText(list.get(position).getStore_mem_integral());
            viewHolder.skuGoodsPlusVipPrice.setText(list.get(position).getPlus_price());
            viewHolder.skuGoodsPlusVipIntergal.setText(list.get(position).getPlus_mem_integral());
            viewHolder.skuGoodsPluCode.setText(list.get(position).getPlu_code());
            viewHolder.skuGoodsOver.setText(list.get(position).getMore_num());
            viewHolder.skuGoodsTint.setText(list.get(position).getLess_num());
            viewHolder.skuGoodsGuideRoyaltyOnlinePrice.setText(list.get(position).getGuide_royalty_online_price());
            viewHolder.skuGoodsGuideRoyaltyPrice.setText(list.get(position).getGuide_royalty_price());
            viewHolder.skuGoodsOnlinePrice.setText(list.get(position).getOnline_price());
            viewHolder.skuGoodsWeightValue.setText(list.get(position).getWeightValue());

            viewHolder.skuGoodsName.setText(name);
            viewHolder.skuGoodsNumber.setText(list.get(position).getFreightNumber());
            viewHolder.skuGoodsCode.setText(list.get(position).getCode());

            if ("1".equals(aCache.getAsString("userType"))){
                viewHolder.skuGoodsPluLin.setVisibility(View.GONE);
            }
            if (isRefrsh) {
                viewHolder.skuGoodsPurPrice.setClickable(false);
                viewHolder.skuGoodsPurPrice.setFocusable(false);
                viewHolder.skuGoodsStock.setClickable(false);
                viewHolder.skuGoodsStock.setFocusable(false);
            }else {
                if (getisSerial()) {
                    viewHolder.skuGoodsStock.setText("");
                    viewHolder.skuGoodsStock.setClickable(false);
                    viewHolder.skuGoodsStock.setFocusable(false);
                } else {
                    viewHolder.skuGoodsStock.setClickable(true);
                    viewHolder.skuGoodsStock.setFocusable(true);
                    viewHolder.skuGoodsStock.setFocusableInTouchMode(true);
//                    viewHolder.skuGoodsStock.requestFocus();
                }
            }
//        if (sellcountBean!=null&&!"1".equals(sellcountBean.getJurisdiction().getShowpurch_price())) {
//            viewHolder.skuGoodsPurPrice.setVisibility(View.GONE);
//        }else {
//            viewHolder.skuGoodsPurPrice.setVisibility(View.VISIBLE);
//        }
            viewHolder.skuGoodsPurPrice.setText("1".equals(aCache.getAsString("userType"))?list.get(position).getPurch_price():list.get(position).getPurchasePrice());
            viewHolder.skuGoodsPrice.setText(list.get(position).getPrice());
            viewHolder.skuGoodsStock.setText(list.get(position).getStock());
            final int index = position;
            final EditText number = viewHolder.skuGoodsNumber;
            number.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    list.get(index).setFreightNumber(number.getText().toString().trim());
                }
            });
            final EditText code = viewHolder.skuGoodsCode;
            code.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    list.get(index).setCode(code.getText().toString().trim());
                }
            });
        setTextListener1(viewHolder.skuGoodsPurPrice,position,"purPrice");
        setTextListener1(viewHolder.skuGoodsPrice,position,"price");
        setTextListener2(viewHolder.skuGoodsStock,position,"stock");
        setTextListener2(viewHolder.skuGoodsWeightValue,position,"weightValue");
        setTextListener1(viewHolder.skuGoodsOnlinePrice,position,"onlinePrice");
        setTextListener1(viewHolder.skuGoodsGuideRoyaltyOnlinePrice,position,"GuideRoyaltyOnlinePrice");
        setTextListener1(viewHolder.skuGoodsGuideRoyaltyPrice,position,"GuideRoyaltyPrice");

            if ("2".equals(aCache.getAsString("platform_type"))) {
                viewHolder.skuLinearQ3.setVisibility(View.VISIBLE);
                viewHolder.skuGoodsQ3.setText(list.get(position).getThird_goods_code());
            } else {
                viewHolder.skuLinearQ3.setVisibility(View.GONE);
            }
            if (editSet.getData().getShow_purchase_price()!=null&&!TextUtils.isEmpty(editSet.getData().getShow_purchase_price())&&
                    Integer.parseInt(editSet.getData().getShow_purchase_price()) == 2) {
                viewHolder.skuGoodsLinPurPrice.setVisibility(View.GONE);
            }else {
                viewHolder.skuGoodsLinPurPrice.setVisibility(View.VISIBLE);
            }
            if (editSet.getData().getSupplier_type()!=null&&!TextUtils.isEmpty(editSet.getData().getSupplier_type())
                    &&!"1".equals(aCache.getAsString("userType"))&&Integer.parseInt(editSet.getData().getSupplier_type())==2)
                viewHolder.llStock.setVisibility(View.GONE);
        if (editSet.getData().getEdit_price()!=null&&!TextUtils.isEmpty(editSet.getData().getEdit_price())&&
                Integer.parseInt(editSet.getData().getEdit_price())==2){
            viewHolder.skuGoodsOnlinePrice.setClickable(false);
            viewHolder.skuGoodsOnlinePrice.setFocusable(false);
            viewHolder.skuGoodsPrice.setClickable(false);
            viewHolder.skuGoodsPrice.setFocusable(false);
            viewHolder.skuGoodsPurPrice.setClickable(false);
            viewHolder.skuGoodsPurPrice.setFocusable(false);
            viewHolder.skuGoodsRetailVipPrice.setClickable(false);
            viewHolder.skuGoodsRetailVipPrice.setFocusable(false);
            viewHolder.skuGoodsShopVipPrice.setClickable(false);
            viewHolder.skuGoodsShopVipPrice.setFocusable(false);
            viewHolder.skuGoodsPlusVipPrice.setClickable(false);
            viewHolder.skuGoodsPlusVipPrice.setFocusable(false);
            viewHolder.skuGoodsRetailVipIntergal.setClickable(false);
            viewHolder.skuGoodsRetailVipIntergal.setFocusable(false);
            viewHolder.skuGoodsShopVipIntergal.setClickable(false);
            viewHolder.skuGoodsShopVipIntergal.setFocusable(false);
            viewHolder.skuGoodsPlusVipIntergal.setClickable(false);
            viewHolder.skuGoodsPlusVipIntergal.setFocusable(false);

//            viewHolder.skuGoodsPriceSet.setVisibility(View.VISIBLE);
        }
        if ((editSet.getData().getIntegral_arrive_cash()!=null&&!TextUtils.isEmpty(editSet.getData().getIntegral_arrive_cash())&&
                Integer.parseInt(editSet.getData().getIntegral_arrive_cash())==2)||
                (editSet.getData().getMarketing_methods()!=null&&!TextUtils.isEmpty(editSet.getData().getMarketing_methods())&&
                        Integer.parseInt(editSet.getData().getMarketing_methods())==1)){
            viewHolder.skuGoodsRetailVipIntergal.setVisibility(View.GONE);
            viewHolder.skuGoodsShopVipIntergal.setVisibility(View.GONE);
            viewHolder.skuGoodsPlusVipIntergal.setVisibility(View.GONE);
        }
        return convertView;
    }


    private  boolean isShopVip;
    private  boolean isRetailVip;
    private  boolean isPlusVip;
    private  boolean isSerial;
    private  boolean isWeight;
    private  boolean isGuide;
    private  EditSet editSet;

    public boolean getisShopVip() {
        return isShopVip;
    }

    public boolean getisRetailVip() {
        return isRetailVip;
    }

    public boolean getisPlusVip() {
        return isPlusVip;
    }

    public boolean getisSerial() {
        return isSerial;
    }

    public boolean getisWeight() {
        return isWeight;
    }

    public boolean getisGuide(){
        return isGuide;
    }


    public void setmStatus(boolean isShopVip,boolean isRetailVip,boolean isPlusVip ) {
        this.isShopVip = isShopVip;
        this.isRetailVip=isRetailVip;
        this.isPlusVip=isPlusVip;
    }

    public void setIsSerial(boolean isSerial ) {
        this.isSerial = isSerial;
    }
    public void setIsWeight(boolean isWeight ) {
        this.isWeight = isWeight;
    }
    public void setshowSet(EditSet editSet) {
        this.editSet = editSet;
    }

    public void setGuide(boolean guide) {
        isGuide = guide;
    }

    private void setTextListener(EditText editText, int index, String flag){
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String text=editText.getText().toString().trim();
                switch (flag){
                    case "shopVipPrice":
                        list.get(index).setShop_mem_price(text);
                        break;
                    case "shopVipIntegral":
                        list.get(index).setShop_mem_integral(text);
                        break;
                    case "VipPrice":
                        list.get(index).setStore_mem_price(text);
                        break;
                    case "VipIntegral":
                        list.get(index).setStore_mem_integral(text);
                        break;
                    case "plusVipPrice":
                        list.get(index).setPlus_price(text);
                        break;
                    case "plusVipIntegral":
                        list.get(index).setPlus_mem_integral(text);
                        break;
                    case "pluCode":
                        list.get(index).setPlu_code(text);
                        break;
                    case "moreNum":
                        list.get(index).setMore_num(text);
                        break;
                    case "lessNum":
                        list.get(index).setLess_num(text);
                        break;
                }
            }
        });
    }

    private void setTextListener1(EditText editText, int index, String flag){
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                String temp = editText.getText().toString();
                int posDot = temp.indexOf(".");
                if (posDot <= 0) return;
                if (temp.length() - posDot - 1 > 2) {
                    editText.setText(temp.substring(0,posDot+3));
                    editText.setSelection(posDot+3);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                String text=editText.getText().toString().trim();
                switch (flag){
                    case "shopVipPrice":
                        list.get(index).setShop_mem_price(text);
                        break;
                    case "VipPrice":
                        list.get(index).setStore_mem_price(text);
                        break;
                    case "plusVipPrice":
                        list.get(index).setPlus_price(text);
                        break;
                    case "GuideRoyaltyPrice":
                        list.get(index).setGuide_royalty_price(text);
                        break;
                    case "GuideRoyaltyOnlinePrice":
                        list.get(index).setGuide_royalty_online_price(text);
                        break;
                    case "onlinePrice":
                        list.get(index).setOnline_price(text);
                        break;
                    case "price":
                        list.get(index).setPrice(text);
                        break;
                    case "purPrice":
                        if ("1".equals(aCache.getAsString("userType"))) {
                            list.get(index).setPurch_price(text);
                        }else {
                            list.get(index).setPurchasePrice(text);
                        }
                        break;
                }
            }
        });
    }
    private void setTextListener2(EditText editText, int index, String flag){
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                String temp = editText.getText().toString();
                int posDot = temp.indexOf(".");
                if (posDot <= 0) return;
                if (temp.length() - posDot - 1 > 3) {
                    editText.setText(temp.substring(0,posDot+4));
                    editText.setSelection(posDot+4);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                String text=editText.getText().toString().trim();
                switch (flag){
                    case "moreNum":
                        list.get(index).setMore_num(text);
                        break;
                    case "lessNum":
                        list.get(index).setLess_num(text);
                        break;
                    case "weightValue":
                        list.get(index).setWeightValue(text);
                        break;
                    case "stock":
                        list.get(index).setStock(text);
                        break;
                }
            }
        });
    }

    static class ViewHolder {
        @BindView(R.id.skuGoods_name)
        TextView skuGoodsName;
        @BindView(R.id.skuGoods_number)
        EditText skuGoodsNumber;
        @BindView(R.id.skuGoods_code)
        EditText skuGoodsCode;
        @BindView(R.id.skuGoods_purPrice)
        EditText skuGoodsPurPrice;
        @BindView(R.id.skuGoods_price)
        EditText skuGoodsPrice;
        @BindView(R.id.skuGoods_stock)
        EditText skuGoodsStock;
        @BindView(R.id.ll_stock)
        LinearLayout llStock;
        @BindView(R.id.skuGoods_weightValue)
        EditText skuGoodsWeightValue;
        @BindView(R.id.ll_weightValue)
        LinearLayout llWeightValue;
        @BindView(R.id.skuGoods_onlinePrice)
        EditText skuGoodsOnlinePrice;
        @BindView(R.id.ll_onlinePrice)
        LinearLayout llOnlinePrice;
        @BindView(R.id.skuLinear_Q3)
        LinearLayout skuLinearQ3;
        @BindView(R.id.skuGoods_Q3)
        EditText skuGoodsQ3;
        @BindView(R.id.skuGoods_shopVipLin)
        LinearLayout skuGoodsShopVipLin;
        @BindView(R.id.skuGoods_shopVipPrice)
        EditText skuGoodsShopVipPrice;
        @BindView(R.id.skuGoods_shopVipIntergal)
        EditText skuGoodsShopVipIntergal;
        @BindView(R.id.skuGoods_retailVipLin)
        LinearLayout skuGoodsRetailVipLin;
        @BindView(R.id.skuGoods_retailVipPrice)
        EditText skuGoodsRetailVipPrice;
        @BindView(R.id.skuGoods_retailVipIntergal)
        EditText skuGoodsRetailVipIntergal;
        @BindView(R.id.skuGoods_plusVipLin)
        LinearLayout skuGoodsPlusVipLin;
        @BindView(R.id.skuGoods_plusVipPrice)
        EditText skuGoodsPlusVipPrice;
        @BindView(R.id.skuGoods_plusVipIntergal)
        EditText skuGoodsPlusVipIntergal;
        @BindView(R.id.skuGoods_plu_code)
        EditText skuGoodsPluCode;
        @BindView(R.id.skuGoods_over)
        EditText skuGoodsOver;
        @BindView(R.id.skuGoods_tint)
        EditText skuGoodsTint;
        @BindView(R.id.skuGoods_status)
        Switch skuGoodsStatus;
        @BindView(R.id.skuGoods_lin_purPrice)
        LinearLayout skuGoodsLinPurPrice;
        @BindView(R.id.sku_PLULin)
        LinearLayout skuGoodsPluLin;
        @BindView(R.id.skuGoods_price_set)
        LinearLayout skuGoodsPriceSet;
        @BindView(R.id.skuGoods_guideRoyaltyOnlinePrice)
        EditText skuGoodsGuideRoyaltyOnlinePrice;
        @BindView(R.id.ll_guideRoyaltyOnlinePrice)
        LinearLayout llGuideRoyaltyOnlinePrice;
        @BindView(R.id.skuGoods_guideRoyaltyPrice)
        EditText skuGoodsGuideRoyaltyPrice;
        @BindView(R.id.ll_guideRoyaltyPrice)
        LinearLayout llGuideRoyaltyPrice;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
